package org.vectrics.common.view;

import org.apache.ecs.html.A;
import org.apache.ecs.html.Div;
import org.apache.ecs.html.Font;
import org.apache.ecs.html.IMG;
import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Row;
import org.vectrics.common.html.Table;
import org.vectrics.common.util.StringUtils;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.user.Person;

public class HtmlFormPersonPickerRow extends HtmlFormRow {
	
	private String remoteMethod = "get.matching.people";
	
	public HtmlFormPersonPickerRow(HtmlForm form, String caption, String paramName, String contextPath, boolean required, int popupLeft, int popupTop) {
		super(form, paramName, required);
        construirPicker( paramName,  caption,  contextPath,  popupLeft,  popupTop);
	}


	public HtmlFormPersonPickerRow(HtmlForm htmlForm, String caption,
			String paramName, String contextPath, boolean required,
			int popupLeft, int popupTop, String remoteMethod) {
		super(htmlForm, paramName, required);
		this.remoteMethod=remoteMethod;
		construirPicker( paramName,  caption,  contextPath,  popupLeft,  popupTop);
	}

	private void construirPicker(String paramName, String caption, String contextPath, int popupLeft, int popupTop) {
    	String inputId = "person_picker_" + paramName;

    	this.getHtmlForm().addHidden(paramName, this.getHtmlForm().getParameterValue(paramName));
    	this.getLabelDiv().addElement(caption);
    	
    	
    	Table table = new Table();
    	table.setCellPadding(0);
    	table.setCellSpacing(0);
    	Row innerRow = table.addRow();
    	Cell innerCell1 = innerRow.addCell();
    	Cell innerCell2 = innerRow.addCell();
    	this.getValueDiv().addElement(table);
    	
    	Div innerDiv = new Div();
    	innerDiv.setID(inputId);
    	innerDiv.setClass(this.getHtmlForm().getStaticClass());
    	innerCell1.addElement(innerDiv);
    	String personId = this.getHtmlForm().getParameterValue(paramName);
    	if (StringUtils.isNullOrBlank(personId) == false) {
    		Person person = CoreServiceLocator.getPersonService().findPersonById(new Long(personId));
    		Font font = new Font();
    		font.setClass("stdFont");
    		font.addElement(person.getFirstName() + " " + person.getLastName());
    		innerDiv.addElement(font);
    	}

	    String href = contextPath + "/admin/user/personSearchDialog.do?selectParam=" + paramName;

	    // Button to pick person
    	A pickLink = new A();
    	pickLink.setHref(href);
    	//String popupWindowId = paramName;
    	pickLink.setOnClick("javascript:personSearchDiv.showPersonSearchDiv('"+remoteMethod+"', 200, 50, 'Find Person', '" +
    			paramName + "');");
    
    	IMG buttonImage = new IMG();
    	buttonImage.setSrc("/hrs-intranet/dynagraphic/button.jpg?code=button.date.pick&caption=Select");
    	buttonImage.setAlt("Pick Person");
    	buttonImage.setBorder(0);
    	buttonImage.setClass("imageButton");
    	buttonImage.setOnClick("javascript:personSearchDiv.showPersonSearchDiv(" +
    			"'"+remoteMethod+"', " +
    			popupLeft + ", " +
    			popupTop + ", '" +
    			caption + "', '" +
    			paramName + "');");

    	innerCell2.addElement(buttonImage);
	}


}
